package nina.yiyou.salary.process.salary

import java.util.stream.Collectors

class ComputeMapMaker {

  ComputeMapMaker(List<List> sourceList) {
    _sourceList = sourceList
  }

  Map make() {
    // 各个表先得出各自额外处理的数据
    List<Map> processList = _sourceList
        .collect { map, proc -> map.collectEntries { k, v -> [k, proc(v)] } }

    // 搜集所有工号准备汇总
    Map<String, Map> initMap = processList.stream()
        .flatMap { it.keySet().stream() }
        .distinct()
        .collect(Collectors.toMap({ it }, { [:] }))

    // 根据工号将各个表的额外结果进行汇总
    return processList.inject(initMap) { sum, val ->
      sum.collectEntries { k, v -> [k, v + (val[k] ?: [:])] }
    }
  }

  private final List<List> _sourceList
}
